package org.ovgu.coursify;

import java.util.ArrayList;
import java.util.List;

import org.ovgu.coursify.data.DataBase;
import org.ovgu.coursify.data.Lecture;
import org.ovgu.coursify.data.Profile;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class HomeActivity extends Activity {
	private final class OnClickListenerDocent implements OnClickListener {
		@Override
		public void onClick(View v) {
			Spinner spinner = (Spinner) findViewById(R.id.coursesSpinner);
			int id = (int) spinner.getSelectedItemId();
			SELECTEDCOURSE = DataBase.getLectures().get(id).getId();

			int count = DataBase.getComments(SELECTEDCOURSE).size();
			if (count == 0) {
				HomeActivity.this.showMessageNoComments();
			} else {
				openCommentars();
			}
		}

	}

	private final class OnClickListenerStudent implements OnClickListener {
		@Override
		public void onClick(View v) {
			Spinner spinner = (Spinner) findViewById(R.id.coursesSpinner);
			int id = (int) spinner.getSelectedItemId();
			SELECTEDCOURSE = Profile.getCurrentProfile().getLectures()[id];
			Intent intentToComment = new Intent(HomeActivity.this,
					CommentAndRateActivity.class);
			intentToComment.putExtra("org.ovgu.coursify.Comment", "COMMENT");
			intentToComment.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intentToComment);
		}
	}

	private final class OnGoToCommentsClick implements OnClickListener {
		@Override
		public void onClick(View v) {
			Spinner spinner = (Spinner) findViewById(R.id.readonlyLecturesSpinner);
			int id = (int) spinner.getSelectedItemId();

			SELECTEDCOURSE = DataBase.getLectures().get(id).getId();

			int count = DataBase.getComments(SELECTEDCOURSE).size();
			if (count == 0) {
				HomeActivity.this.showMessageNoComments();
			} else {
				openCommentars();
			}
		}

	}

	private static Context context;

	private static HomeActivity h;

	public static int SELECTEDCOURSE;

	public static Context getContext() {
		return context;
	}

	public static void updateOnLoggedIn() {
		boolean isStudent = Profile.getCurrentProfile().getType()
				.equals(Profile.TYPE_STUDENT);

		List<String> lecturesNames = new ArrayList<String>();
		List<Lecture> lectures = isStudent ? DataBase.getLectures(Profile
				.getCurrentProfile()) : DataBase.getLectures();

		for (Lecture lecture : lectures) {
			lecturesNames.add(lecture.getName() + "\n" + lecture.getDocent());
		}

		Spinner spinner = (Spinner) h.findViewById(R.id.coursesSpinner);
		spinner.setAdapter(new ArrayAdapter<String>(HomeActivity.getContext(),
				R.layout.spinner_item, lecturesNames));

		Button button = (Button) h.findViewById(R.id.commentButt);
		if (isStudent) {
			// SELECTEDCOURSE = lectures.get(0).getId();
			button.setOnClickListener(h.onClickListenerStudent);
			button.setText("Comment and rate");
		} else {
			// SELECTEDCOURSE = DataBase.getLectures().get(0).getId();
			button.setOnClickListener(h.onClickListenerDocent);
			button.setText("Browse comments");
		}
		Spinner readOnlyLecturesSpinner = (Spinner) h
				.findViewById(R.id.readonlyLecturesSpinner);
		Button goToCommentsButton = (Button) h
				.findViewById(R.id.goToCommentsButton);
		// show, if student is logged in, all lectures and short cut to their
		// comments
		if (isStudent) {
			lecturesNames = new ArrayList<String>();
			List<Lecture> allLectures = DataBase.getLectures();

			for (Lecture lecture : allLectures) {
				lecturesNames.add(lecture.getName() + "\n"
						+ lecture.getDocent());
			}
			readOnlyLecturesSpinner.setAdapter(new ArrayAdapter<String>(
					context, R.layout.spinner_item, lecturesNames));
			readOnlyLecturesSpinner.setVisibility(View.VISIBLE);
			goToCommentsButton.setVisibility(View.VISIBLE);
		} else {
			readOnlyLecturesSpinner.setVisibility(View.INVISIBLE);
			goToCommentsButton.setVisibility(View.INVISIBLE);
		}

	}

	private OnClickListener onClickListenerDocent = new OnClickListenerDocent();
	private OnClickListener onClickListenerStudent = new OnClickListenerStudent();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		h = this;
		context = getApplicationContext();

		if (!Profile.isLoggedIn()) {
			showLogIn();
		}

		setContentView(R.layout.activity_home);
		Button goToCommentsButton = (Button) h
				.findViewById(R.id.goToCommentsButton);
		goToCommentsButton.setOnClickListener(new OnGoToCommentsClick());
		if (Profile.isLoggedIn()) {
			updateOnLoggedIn();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.home, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_logout:
			Profile.logout();
			showLogIn();
			break;
		default:
			return super.onOptionsItemSelected(item);
		}
		return true;
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		if (Profile.isLoggedIn()) {
			TextView view = (TextView) findViewById(R.id.helloUser);
			view.setText("Hello, " + Profile.getCurrentProfile().getName());
		}
	}

	private void openCommentars() {
		Intent ide = new Intent(HomeActivity.this, ViewComments.class);
		ide.putExtra("org.ovgu.coursify.viewComments", "ViEWCOMMENT");
		ide.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(ide);
	}

	private void showLogIn() {
		Intent ide = new Intent(HomeActivity.this, LoginActivity.class);
		ide.putExtra("org.ovgu.coursify.LogIn", "LOGIN");
		ide.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(ide);
	}

	private void showMessageNoComments() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("No comments")
				.setMessage("This lecture has no comments yet.")
				.setIcon(android.R.drawable.btn_dialog)
				.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}
}
